package com.jeasonzhao.web;

import java.io.IOException;
import java.io.Writer;

public interface IHtmlWriter
{
    public void write(CharSequence str)
        throws java.io.IOException;

    public void disposeWriter();

    public static final IHtmlWriter Dummy = new IHtmlWriter()
    {
        public void write(CharSequence str)
            throws java.io.IOException
        {

        }

        public void disposeWriter()
        {

        }
    };
    public static class StringWriter implements IHtmlWriter
    {
        private StringBuffer m_buf = new StringBuffer();
        public StringWriter()
        {

        }

        public void write(CharSequence str)
            throws IOException
        {
            if(null == str)
            {
                return;
            }
            if(null == m_buf)
            {
                m_buf = new StringBuffer();
            }
            m_buf.append(str);
        }

        public String toString()
        {
            return null != m_buf ? m_buf.toString() : "";
        }

        public void disposeWriter()
        {
        }
    }

    public static class FileHtmlWriter implements IHtmlWriter
    {
        private Writer m_writer = null;
        public FileHtmlWriter(java.io.File file)
            throws IOException
        {
            m_writer = new java.io.FileWriter(file);
        }

        public FileHtmlWriter(String fileName)
            throws IOException
        {
            m_writer = new java.io.FileWriter(fileName);
        }

        public void write(CharSequence str)
            throws IOException
        {
            if(null != m_writer && null != str)
            {
                m_writer.write(str.toString());
                m_writer.flush();
            }
        }

        public void close()
        {
            try
            {
                if(null != m_writer)
                {
                    m_writer.close();
                    m_writer = null;
                }
            }
            catch(IOException ex)
            {
            }
        }

        public void disposeWriter()
        {
            this.close();
        }
    }

    public static class WriterHtmlWriter implements IHtmlWriter
    {
        private Writer m_writer = null;
        public WriterHtmlWriter(Writer stream)
        {
            m_writer = stream;
        }

        public void write(CharSequence str)
            throws IOException
        {
            if(null != m_writer && null != str)
            {
                m_writer.write(str.toString());
                m_writer.flush();
            }
        }

        public void disposeWriter()
        {
        }

    }

    public static class StreamHtmlWriter implements IHtmlWriter
    {
        private java.io.OutputStream m_stream = null;
        public StreamHtmlWriter(java.io.OutputStream stream)
        {
            m_stream = stream;
        }

        public void write(CharSequence str)
            throws IOException
        {
            if(null != m_stream && null != str)
            {
                m_stream.write(str.toString().getBytes());
                m_stream.flush();
            }
        }

        public void disposeWriter()
        {
        }
    }

}
